package com.higgins.lawapp.client.settings.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.model.DataSourceModel;
import com.higgins.lawapp.client.settings.panels.SettingsInternalTabs;
import com.higgins.lawapp.client.settings.panels.SettingsInternalTabsPanel;


public class ButtonsPanel extends LayoutPanel	{
	private final ButtonsPanel buttonsPanel = this;
	private SettingsInternalTabsPanel settingsInternalTabsPanel;
	private DataSourceModel dataSourceModel;
	private SettingsInternalTabs settingsInternalTabs;
	private List<DataSourceComponent> panelList;
	private int tabNumber;
	private DataItemTable dataItemTable;
	private String nextTabURL;
	
	public ButtonsPanel(DataSourceModel dataSourceModel, SettingsInternalTabs settingsInternalTabs, 
			SettingsInternalTabsPanel settingsInternalTabsPanel, List<DataSourceComponent> panelList, int tabNumber)	{
		this.settingsInternalTabsPanel = settingsInternalTabsPanel;
		this.dataSourceModel = dataSourceModel;
		this.settingsInternalTabs = settingsInternalTabs;
		this.panelList = panelList;
		this.tabNumber = tabNumber;
		instantiateButtons();
	}
	
	public void instantiateButtons()	{
		Button labelButton = new Button("add Label");
		labelButton.addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  addLabelPanel();
		        }
		      });
      
		Button elementButton = new Button("add Element");
		elementButton.addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  addElementPanel();		        }
		      });
    
		Button elementFieldButton = new Button("add Field");
		elementFieldButton.addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		    	  addFieldPanel();
		        }
		      });

		Button saveButton = new Button("Save");
		saveButton.addClickHandler(settingsInternalTabs);

		Button executeButton = new Button("Execute");
		executeButton.addClickHandler(new ClickHandler() {
		      public void onClick(ClickEvent event) {
		          dataSourceModel.executeScreenScraping(tabNumber, buttonsPanel);
		        }
		      });
    		

		add(labelButton);
	    add(elementButton);
	    add(elementFieldButton);
	    add(saveButton);
	    add(executeButton);
	    setWidgetLeftWidth(labelButton, 0, Unit.PCT, 20, Unit.PCT);
	    setWidgetLeftWidth(elementButton, 20, Unit.PCT, 20, Unit.PCT);
	    setWidgetLeftWidth(elementFieldButton, 40, Unit.PCT, 20, Unit.PCT);
	    setWidgetLeftWidth(saveButton, 60, Unit.PCT, 20, Unit.PCT);
	    setWidgetRightWidth(executeButton, 0, Unit.PCT, 20, Unit.PCT);
	}
	
	public void addLabelPanel()	{
		addLabelPanel("", 1);
	}
	public void addLabelPanel(String label, Integer number)	{
		DataSourceLabelFields dataSourceLabelFields = new DataSourceLabelFields(label, number);
	    panelList.add(dataSourceLabelFields);
	    settingsInternalTabsPanel.addNorth(dataSourceLabelFields, 2);
	    dataSourceLabelFields.setVisible(true);
	}		
	
	// Create Element Panel
	public void addElementPanel()	{
		addElementPanel("", "");
	}
	
	public void addElementPanel(String start, String end)	{
		DataSourceElementFields dataSourceElementFields = new DataSourceElementFields(start, end);
	    panelList.add(dataSourceElementFields);
		settingsInternalTabsPanel.addNorth(dataSourceElementFields, 2);
	}	
	
	// Create Field Panel
	public void addFieldPanel()	{
		addFieldPanel("", "", "", false, false);
	}
	public void addFieldPanel(String fieldName, String start, String end, Boolean url, Boolean concat)	{
		DataSourceFieldFields dataSourceFieldFields = new DataSourceFieldFields(fieldName, start, end, url, concat);
	    panelList.add(dataSourceFieldFields);
	    settingsInternalTabsPanel.addNorth(dataSourceFieldFields, 2);
	}	
	
	public void addResultsTable(List<DataItemDTO> dataItems)	{
		DataSourceDTO dataSourceDTO = dataSourceModel.getDataSourceDTOSelected();
		DataSourceLevelDTO dataSourceLevelDTO = dataSourceDTO.getDataSourceLevelDTO(tabNumber);
		String dataSourceURL=clearDownURL(dataSourceLevelDTO);
		
	    dataItemTable = new DataItemTable(dataSourceModel);
	    settingsInternalTabsPanel.addNorth(dataItemTable, 200);
	    dataItemTable.addData(dataItems);

	    // If highest tab number, then create new tab
	    if (dataSourceDTO.getDataSourceLevelDTOs().size() <= (tabNumber+1))	{
		    Iterator<DataSourceComponent> iterator = panelList.iterator();
		    int fieldIndex = 0;
		    while (iterator.hasNext())	{
		    	DataSourceComponent dataSourceComponent = iterator.next();
		    	String dataSourceComponentType = dataSourceComponent.getComponentType();
		    	if (dataSourceComponentType.equals("Field"))	{
		    		if (((DataSourceFieldFields)dataSourceComponent).isURL())	{
		    			String newTabName=((DataSourceFieldFields)dataSourceComponent).getFieldName();
	    				nextTabURL=getNextTabURL(dataItems, fieldIndex);
		    	        settingsInternalTabs.createSettingsInternalTabsPanel(newTabName, dataSourceURL, nextTabURL, null, tabNumber+1);	
		    	        DataSourceLevelDTO newDataSourceLevelDTO = new DataSourceLevelDTO();
		    	        dataSourceModel.addDataSourceLevelDTO(newDataSourceLevelDTO);
		    		}
		    	fieldIndex++;
		    	}
		    }
	    }
	}

	private String getNextTabURL(List<DataItemDTO> dataItemLevelDTO, int fieldIndex)	{
		nextTabURL="";
		List<DataItemDTO> dataItemDTOs = new ArrayList<DataItemDTO>();
		if (dataItemLevelDTO!=null)
			dataItemDTOs = dataItemLevelDTO;
		if (dataItemDTOs.size()==0)
			nextTabURL = "";
		else	{
			switch(fieldIndex)	{
			case 0:
				nextTabURL = dataItemDTOs.get(0).getItemOne();	
				break;
			case 1:
				nextTabURL = dataItemDTOs.get(0).getItemTwo();	
				break;
			case 2:
				nextTabURL = dataItemDTOs.get(0).getItemThree();	
				break;
			case 3:
				nextTabURL = dataItemDTOs.get(0).getItemFour();	
				break;
			case 4:
				nextTabURL = dataItemDTOs.get(0).getItemFive();	
				break;
			default:
				nextTabURL = "";
			}
		}
		return nextTabURL;
	}
	
	public String clearDownURL(DataSourceLevelDTO dataSourceLevelDTO)	{
		String url=dataSourceLevelDTO.getDataSourceURL();
		
		return url;
	}
}
